package com.jzr.task.job.kangying;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.RestTemplate;

public class KyRestTemplateUtil {
    public static<T> T postJson(RestTemplate restTemplate, String url, Object requestObject, Class<T> responseType) throws Exception {
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        headers.add("Accpet-Encoding", "gzip");
        headers.add("Content-Encoding", "UTF-8");

        HttpEntity<String> request = new HttpEntity<>(KyJasonUtil.toJson(requestObject),headers);

        try {
            return restTemplate.postForEntity(url, request, responseType).getBody();
        }catch (HttpStatusCodeException e){
            throw new Exception(e.getStatusText() + "," + e.getResponseBodyAsString());
        }
    }
}
